package com.zpb.framework.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zpb.framework.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/13
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Long currentId;
        try {
            currentId = SecurityUtils.getCurrentId();
        } catch (Exception e) {
            e.printStackTrace();
            currentId = -1L;
        }

        this.strictInsertFill(metaObject, "createBy", Long.class, currentId);
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictUpdateFill(metaObject,"updateBy",Long.class, currentId);
        this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject,"updateBy",Long.class, SecurityUtils.getCurrentId());
        this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
    }
}
